﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Enum;
using Bot.Domain.Specifications;

namespace Bot.Domain.Categories.Service
{
    public class SmartTriggerActionDomainService : ISmartTriggerActionDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, SmartTriggerAction> _repository;

        public SmartTriggerActionDomainService(IRepository<Guid, SmartTriggerAction> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public SmartTriggerAction Create(SmartTriggerAction action)
        {           
            if (action.TargetType == EnumTargetType.agent && action.SelectedAgents != null && action.SelectedAgents.Count>0)
            {
                ISmartTriggerActionSelectedAgentDomainService agentDomainService = _container.Resolve<ISmartTriggerActionSelectedAgentDomainService>();
                foreach (var agentId in action.SelectedAgents)
                {
                    agentDomainService.Create(new SmartTriggerActionSelectedAgent()
                    {
                        Id = Guid.NewGuid(),
                        AgentId = agentId,
                        SmartTriggerActionId = action.Id
                    });
                }
            }
            else if (action.TargetType == EnumTargetType.department && action.SelectedDepartments != null && action.SelectedDepartments.Count > 0)
            {
                ISmartTriggerActionSelectedDepartmentDomainService departmentDomainService = _container.Resolve<ISmartTriggerActionSelectedDepartmentDomainService>();
                foreach (var departmentId in action.SelectedDepartments)
                {
                    departmentDomainService.Create(new SmartTriggerActionSelectedDepartment()
                    {
                        Id = Guid.NewGuid(),
                         DepartmentId = departmentId,
                        SmartTriggerActionId = action.Id
                    });
                }
            }
            else if (action.TargetType == EnumTargetType.visitorSegment && action.SelectedVisitorSegments != null && action.SelectedVisitorSegments.Count > 0)
            {
                ISmartTriggerActionSelectedVisitorSegmentDomainService segmentDomainService = _container.Resolve<ISmartTriggerActionSelectedVisitorSegmentDomainService>();
                foreach (var segmentId in action.SelectedVisitorSegments)
                {
                    segmentDomainService.Create(new SmartTriggerActionSelectedVisitorSegment()
                    {
                        Id = Guid.NewGuid(),
                        VisitorSegmentId = segmentId,
                        SmartTriggerActionId = action.Id
                    });
                }
            }
            _repository.Create(action);
            return Get(action.Id);
        }

        public SmartTriggerAction Get(Guid id)
        {
            var result = _repository.Get(id);
            if (result.TargetType == EnumTargetType.agent)
            {
                ISmartTriggerActionSelectedAgentDomainService agentDomainService = _container.Resolve<ISmartTriggerActionSelectedAgentDomainService>();
                result.SelectedAgents = agentDomainService.GetByActionId(id).Select(a => a.AgentId).ToList();
                result.SelectedDepartments = null;
                result.SelectedVisitorSegments = null;
            }
            else if (result.TargetType == EnumTargetType.department)
            {
                ISmartTriggerActionSelectedDepartmentDomainService departmentDomainService = _container.Resolve<ISmartTriggerActionSelectedDepartmentDomainService>();
                result.SelectedDepartments = departmentDomainService.GetByActionId(id).Select(a => a.DepartmentId).ToList();
                result.SelectedAgents = null;
                result.SelectedVisitorSegments = null;
            }
            else if (result.TargetType == EnumTargetType.visitorSegment)
            {
                ISmartTriggerActionSelectedVisitorSegmentDomainService segmentDomainService = _container.Resolve<ISmartTriggerActionSelectedVisitorSegmentDomainService>();
                result.SelectedVisitorSegments = segmentDomainService.GetByActionId(id).Select(a => a.VisitorSegmentId).ToList();
                result.SelectedDepartments = null;
                result.SelectedAgents = null;
            }
            return result;
        }

        public SmartTriggerAction Update(SmartTriggerActionUpdateBo bo)
        {
            SmartTriggerAction smartTriggerAction = _repository.Get(bo.Id);
            //smartTriggerAction.Id = bo.Id;
            //smartTriggerAction.SmartTriggerId = bo.SmartTriggerId;
            smartTriggerAction.Type = bo.Type;
            smartTriggerAction.IsEnabled = bo.IsEnabled;
            smartTriggerAction.AgentOfflineMessage = bo.AgentOfflineMessage;
            smartTriggerAction.TargetType = bo.TargetType;
            smartTriggerAction.SelectedAgents = bo.SelectedAgents;
            smartTriggerAction.SelectedDepartments = bo.SelectedDepartments;
            smartTriggerAction.SelectedVisitorSegments = bo.SelectedVisitorSegments;

            if (bo.TargetType == EnumTargetType.agent && bo.SelectedAgents != null && bo.SelectedAgents.Count > 0)
            {
                ISmartTriggerActionSelectedAgentDomainService agentDomainService = _container.Resolve<ISmartTriggerActionSelectedAgentDomainService>();
                var agentIds = agentDomainService.GetByActionId(bo.Id);
                foreach (var agentId in bo.SelectedAgents)
                {
                    if (agentIds.FirstOrDefault(a => a.AgentId == agentId) == null)
                    {
                        //create
                        agentDomainService.Create(new SmartTriggerActionSelectedAgent()
                        {
                            Id = Guid.NewGuid(),
                            AgentId = agentId,
                            SmartTriggerActionId = bo.Id
                        });
                    }
                }
                //delete
                foreach(var deletedItem in agentIds)
                {
                    if (!bo.SelectedAgents.Contains(deletedItem.AgentId) )
                    {
                        agentDomainService.Delete(deletedItem.Id);
                    }
                }
            }
            else if (bo.TargetType == EnumTargetType.department && bo.SelectedDepartments != null && bo.SelectedDepartments.Count > 0)
            {
                ISmartTriggerActionSelectedDepartmentDomainService departmentDomainService = _container.Resolve<ISmartTriggerActionSelectedDepartmentDomainService>();
                var departmentIds = departmentDomainService.GetByActionId(bo.Id);
                foreach (var departmentId in bo.SelectedDepartments)
                {
                    if (departmentIds.FirstOrDefault(a => a.DepartmentId == departmentId) == null)
                    {
                        //create
                        departmentDomainService.Create(new SmartTriggerActionSelectedDepartment()
                        {
                            Id = Guid.NewGuid(),
                            DepartmentId = departmentId,
                            SmartTriggerActionId = bo.Id
                        });
                    }
                }
                //delete
                foreach (var deletedItem in departmentIds)
                {
                    if (!bo.SelectedDepartments.Contains(deletedItem.DepartmentId))
                    {
                        departmentDomainService.Delete(deletedItem.Id);
                    }
                }
            }
            else if (bo.TargetType == EnumTargetType.visitorSegment && bo.SelectedVisitorSegments != null && bo.SelectedVisitorSegments.Count > 0)
            {
                ISmartTriggerActionSelectedVisitorSegmentDomainService segmentDomainService = _container.Resolve<ISmartTriggerActionSelectedVisitorSegmentDomainService>();
                var segmentIds = segmentDomainService.GetByActionId(bo.Id);
                foreach (var segmentId in bo.SelectedVisitorSegments)
                {
                    if (segmentIds.FirstOrDefault(a => a.VisitorSegmentId == segmentId) == null)
                    {
                        //create
                        segmentDomainService.Create(new SmartTriggerActionSelectedVisitorSegment()
                        {
                            Id = Guid.NewGuid(),
                            VisitorSegmentId = segmentId,
                            SmartTriggerActionId = bo.Id
                        });
                    }
                }
                //delete
                foreach (var deletedItem in segmentIds)
                {
                    if (!bo.SelectedVisitorSegments.Contains(deletedItem.VisitorSegmentId))
                    {
                        segmentDomainService.Delete(deletedItem.Id);
                    }
                }
            }

            _repository.Update(smartTriggerAction);

            return smartTriggerAction;
        }

        public IEnumerable<SmartTriggerAction> List()
        {
            return _repository.ListAll();
        }

        public IEnumerable<SmartTriggerAction> GetBySmartTriggerId(Guid smartTriggerId)
        {
            var list = _repository.List(new SmartTriggerActionFilterSpecification(smartTriggerId));
            if (list != null && list.Count > 0)
            {
                foreach (var result in list)
                {
                    if (result.TargetType == EnumTargetType.agent)
                    {
                        ISmartTriggerActionSelectedAgentDomainService agentDomainService = _container.Resolve<ISmartTriggerActionSelectedAgentDomainService>();
                        result.SelectedAgents = agentDomainService.GetByActionId(result.Id).Select(a => a.AgentId).ToList();
                        result.SelectedDepartments = null;
                        result.SelectedVisitorSegments = null;
                    }
                    else if (result.TargetType == EnumTargetType.department)
                    {
                        ISmartTriggerActionSelectedDepartmentDomainService departmentDomainService = _container.Resolve<ISmartTriggerActionSelectedDepartmentDomainService>();
                        result.SelectedDepartments = departmentDomainService.GetByActionId(result.Id).Select(a => a.DepartmentId).ToList();
                        result.SelectedAgents = null;
                        result.SelectedVisitorSegments = null;
                    }
                    else if (result.TargetType == EnumTargetType.visitorSegment)
                    {
                        ISmartTriggerActionSelectedVisitorSegmentDomainService segmentDomainService = _container.Resolve<ISmartTriggerActionSelectedVisitorSegmentDomainService>();
                        result.SelectedVisitorSegments = segmentDomainService.GetByActionId(result.Id).Select(a => a.VisitorSegmentId).ToList();
                        result.SelectedDepartments = null;
                        result.SelectedAgents = null;
                    }
                }
            }
            return list;
        }

        public void Delete(Guid id)
        {
            SmartTriggerAction smartTriggerAction = _repository.Get(id);

            _repository.Delete(smartTriggerAction);

        }
    }
}
